import Vue from 'vue'
import VueRouter from 'vue-router'

import NProgress from 'nprogress'
import 'nprogress/nprogress.css' // 引入基础样式

import index from '@/views/client/index.vue'
import adminIndex from "@/views/admin/adminIndex.vue";
import userInfoIndex from "@/views/client/user/userInfoIndex.vue";

Vue.use(VueRouter)

const routes = [
  {
    path: "/index",
    name: "index",
    redirect: '/paper', // 默认跳转到模块1
  },
  {
    path: "/",
    name: "index",
    component: index,
    redirect: '/paper', // 默认跳转到模块1
    children: [
      {
        path: "paper", // 实际路径为 /paper
        name: "paper",
        component: () => import("@/views/client/modules/paperWeightReduction.vue")
      },
      {
        path: "module2", // 实际路径为 /module2
        name: "module2",
        component: () => import("@/views/client/modules/module2.vue")
      },
      {
        path: "module3", // 实际路径为 /module3
        name: "module3",
        component: () => import("@/views/client/modules/module3.vue")
      },
      {
        path: "user", // 实际路径为 /module3
        name: "user",
        component:userInfoIndex,
        children:[
            {
              path: "userProfile", // 实际路径为 /module3
              name: "userProfile",
              component: () => import("@/views/client/user/userProfile.vue")
            },
        ]
      },
    ]
  },
  {
    path: "/kunIndex",
    name: "kunAdminIndex",
    component: adminIndex
  },
  {
    path: "/kun",
    name: "loginAdmin",
    component:() => import("@/components/loginAdmin.vue")
  },
  {
    path: '*',
    name: 'notFound',
    component: () => import('@/components/NotFound.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


// 配置 NProgress（可选）
NProgress.configure({
  easing: 'ease',      // 动画方式
  speed: 500,          // 递增进度条速度
  showSpinner: false,  // 是否显示加载图标
  trickleSpeed: 200,   // 自动递增间隔
  minimum: 0.3         // 初始化时的最小百分比
})

// 路由守卫配置
router.beforeEach((to, from, next) => {
  NProgress.start() // 开始加载进度条
  next()
})
router.afterEach(() => {
  NProgress.done() // 完成进度条
})
// 处理路由加载错误
router.onError((error) => {
  NProgress.done() // 确保出错时进度条消失
  console.error('路由错误:', error)
})

//解决vue-router重复点击报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) { return originalPush.call(this, location).catch((err) => err) }
export default router
